package com.ch999.detect.View.activity;

import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.ch999.detect.AppBaseActivity;
import com.ch999.detect.R;
import com.ch999.detect.View.widget.TouchChangeColorView;
import com.ch999.util.StatusBarUtil;
import com.github.mzule.activityrouter.annotation.Router;
import com.scorpio.mylib.ottoBusProvider.BusEvent;
import com.scorpio.mylib.ottoBusProvider.BusProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhulang on 2017/12/28.
 */

@Router("ontouch")
public class OnTouchActivty extends AppBaseActivity implements TouchChangeColorView.OnTouchMotionUpListenter, View.OnTouchListener,View.OnClickListener {
    int[] ids = new int[]{

            R.id.ll_group_1, R.id.ll_group_1_1, R.id.ll_group_1_2, R.id.ll_group_1_3,
            R.id.ll_group_2, R.id.ll_group_2_1, R.id.ll_group_2_2, R.id.ll_group_2_3,
            R.id.ll_group_3, R.id.ll_group_3_1, R.id.ll_group_3_2, R.id.ll_group_3_3,
            R.id.ll_group_4, R.id.ll_group_4_1, R.id.ll_group_4_2, R.id.ll_group_4_3,
            R.id.ll_group_5

    };

    List<LinearLayout> viewGroup;

    Button btn_warning;
    List<TouchChangeColorView> touchViews = new ArrayList<>();

    public int touchNum = 0;

    OnTouchActivty activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    private void initData() {
        int statusBarHeight = StatusBarUtil.getStatusBarHeight(mContext);
        int size = viewGroup.size();
        if (size > 0) {

            for (LinearLayout linearViwe : viewGroup) {

                int childCount = linearViwe.getChildCount();
                if (childCount > 0) {

                    for (int i = 0; i < childCount; i++) {

                        View childAt = linearViwe.getChildAt(i);
                        if (childAt != null && childAt instanceof TouchChangeColorView) {

                            TouchChangeColorView touchChangeColorView = (TouchChangeColorView) childAt;
                            touchChangeColorView.setOnTouchMotionUpListenter(this,statusBarHeight);
                            touchViews.add(touchChangeColorView);
                        }

                    }
                }
            }
        }



    }


    @Override
    public void onTouch(TouchChangeColorView view) {

        touchNum++;

        if (touchNum == touchViews.size()) {
            //检测完成
            BusEvent event = new BusEvent();
            event.setContent("0");
            event.setAction(NewDetectActivity.BUSACTION_ONTOUCH_SUCCESS);
            BusProvider.getInstance().post(event);
            finish();
        }

    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int action = MotionEventCompat.getActionMasked(event);

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                return true;

            case MotionEvent.ACTION_MOVE:

                for (TouchChangeColorView v : touchViews) {
                    v.inOnTuchEvent(event.getX(), event.getY());
                }

                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return false;

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_warning) {
            BusEvent event = new BusEvent();
            event.setContent("1");
            event.setAction(NewDetectActivity.BUSACTION_ONTOUCH_SUCCESS);
            BusProvider.getInstance().post(event);
            finish();
        }

    }

    @Override
    public int getLayoutId() {
         return R.layout.fragment_test_touch;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        viewGroup = new ArrayList<>();
        for(int id:ids){
            viewGroup.add((LinearLayout) findViewById(id));
        }

        btn_warning= (Button)findViewById(R.id.btn_warning);
        btn_warning.setOnClickListener(this);
        initData();

        findViewById(R.id.ll_root_view).setOnTouchListener(this);
    }

    @Override
    public void findView() {

    }
}
